package models

import (
	"database/sql"
	"time"

	"golang.org/x/crypto/bcrypt"
)

type User struct {
	ID        int
	FirstName string
	LastName  string
	Mobile    sql.NullString `gorm:"not null;unique"`
	Email     sql.NullString `gorm:"not null;unique"`
	Pass      string
	CreatedAt time.Time
	UpdatedAt time.Time
}

func (User) TableName() string {
	return "user_base"
}

// GetUser 用ID获取用户
func GetUser(id interface{}) (User, error) {
	var user User
	result := DB.First(&user, id)
	return user, result.Error
}

// SetPassword 设置密码
func SetUserPassword(id int, password string) error {
	bytes, err := bcrypt.GenerateFromPassword([]byte(password), 12)
	if err != nil {
		return err
	}

	user := User{ID: id}
	DB.Model(&user).Update("pass", string(bytes))
	return nil
}

// CheckPassword 校验密码
/* func CheckPassword(password string) bool {
	err := bcrypt.CompareHashAndPassword([]byte(user.PasswordDigest), []byte(password))
	return err == nil
} */
